<?php

namespace App\Http\Controllers;

use App\Models\FangOwner;
use Illuminate\Http\Request;

class FangOwnerController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = (new FangOwner())->getFangOwnerAttrAll();
        return view('admin.fang_owner.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.fang_owner.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $params = $request->except(['_token','file']);
        //print_r($params);die();
        $res =  FangOwner::create($params);
        if ($res){
            return redirect(route('admin.fang_owner.index'));
        }else{
            return redirect(route('admin.fang_owner.create'));
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function localUploadFile(Request $request){
        $pic = $request->file('file')->store('', 'fang_owner');
        $picPath = '/uploads/fang_owner/' . $pic;
        return $this->writeJson(0, ['pic' => $picPath], 'success');
    }

    public function DelFile(Request $request)
    {
        $pic = $request->get('pic');
        //真实的删除图片 unlink 必须是绝对路径
        $res = unlink(public_path() . $pic);
        if ($res) {
            return $this->writeJson(0, null, 'success');
        } else {
            //写入失败，还在当前页，来一个友好提示
            return $this->writeJson(20003, null, 'error');
        }
    }
}
